package com.uniledger.exchange.base.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.TextView;

import butterknife.ButterKnife;
import butterknife.Unbinder;

public abstract class BaseCoreFragment extends Fragment {

    public View rootView;
    public LayoutInflater inflater;

    private Unbinder unbinder;

    @Nullable
    @Override
    public final View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        this.inflater = inflater;
        if (rootView == null) {
            rootView = inflater.inflate(this.getViewResourceId(), container, false);
            unbinder = ButterKnife.bind(this, rootView);
            parseArguments(getArguments());
            initView();
            initData();
        }
        ViewGroup parent = (ViewGroup) rootView.getParent();
        if (parent != null) {
            parent.removeView(rootView);
        }
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (unbinder != null) {
//            unbinder.unbind();
        }
    }

    /**
     * 隐藏键盘
     */
    protected void dismissKeyboard(TextView textView) {
        InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(textView.getWindowToken(), 0);
    }

    public View findViewById(@IdRes int id) {
        return rootView.findViewById(id);
    }

    protected abstract void parseArguments(Bundle bundle);

    protected abstract void initData();

    public abstract int getViewResourceId();

    protected abstract void initView();
}
